package com.burgos.inventory;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SearchMenu extends Activity implements OnClickListener {

	private EditText name_code;
	private Button search;
	private ProgressDialog pDialog;
	JSONParser jsonParser = new JSONParser();
	private static final String LOGIN_URL = "http://test1984.site11.com/remote/searchproduct.php";
	private static final String TAG_SUCCESS = "success";
	private static final String TAG_MESSAGE = "message";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_product);
		
		name_code = (EditText) findViewById(R.id.inputsearch);
		search = (Button) findViewById(R.id.gosearch);
		search.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		//We pressed search product
		switch (v.getId()) {
		case R.id.gosearch:
			new TrySearch().execute();
			break;
		default:
			break;
		}
	}
	
	class TrySearch extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(SearchMenu.this);
			pDialog.setMessage("Searching Product, please wait...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		@Override
		protected String doInBackground(String... args) {
			int success;
			String input = name_code.getText().toString();
			//If we did not input any string
			if(input.equals("")){
				return "Enter a name or a code";
			}
			//we build the parameters and make an http request
			try {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("keyword", input));
				JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",	params);
				success = json.getInt(TAG_SUCCESS);
				//If succeed we pass the string to the next activity
				//this method only verifies that the product exists on DB
				if (success == 1) {
					Intent i = new Intent(SearchMenu.this, Product.class);
					i.putExtra("keyword",input);
					finish();
					startActivity(i);
				} else {
					return json.getString(TAG_MESSAGE);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}

		protected void onPostExecute(String file_url) {
			pDialog.dismiss();
			if (file_url != null) {
				Toast.makeText(SearchMenu.this, file_url, Toast.LENGTH_LONG).show();
			}
		}
	}
}
